package com.taomeishu.mailcrazy.fetch.policy;

import java.util.Random;


public class RandomDelayPolicy implements DelayPolicy {

	private int minInMillisSeconds;
	
	private int maxInMillisSeconds;
	
	private Random random = new Random(System.currentTimeMillis());
	
	public RandomDelayPolicy(int minInSeconds, int maxInSeconds) {
		super();
		this.minInMillisSeconds = 1000*Math.min(minInSeconds, maxInSeconds);
		this.maxInMillisSeconds = 1000*Math.max(minInSeconds, maxInSeconds);
	}

	protected int sleepInMillisSeconds() {
		return random.nextInt(maxInMillisSeconds - minInMillisSeconds + 1) + minInMillisSeconds;
	}
	
	@Override
	public void sleepForDelay() throws InterruptedException {
		Thread.sleep(sleepInMillisSeconds());
	}
	
	public static void main(String[] args) {
		RandomDelayPolicy r = new RandomDelayPolicy(1, 2);
		for (int i = 0; i < 100; ++i) {
			System.out.println(r.sleepInMillisSeconds());
		}
	}
	
}
